from django.shortcuts import render,redirect
from .models import Information
from .models import Type
from django.http import HttpResponse
import os

# Create your views here.
def Z1(request):
    list=Information.objects.all()
    content={'list':list}
    return render(request,'Z1.html',content)
def Z2(request):
    return render(request,'Z2.html')
def Z2_handler(request):
    title=request.POST.get('title')
    content=request.POST.get('content')
    typename=request.POST.get('typename')
    files = request.FILES.get('file')
    power=request.POST.get('power')
    file=open(os.path.join('static','Image',files.name),'wb')
    for fp in files.chunks():
            file.write(fp)
    file.close()
    Z=Information()
    Z.title=title
    Z.content=content
    Z.types_id=Type.objects.filter(typename = typename)[0].id
    Z.route= os.path.join('static','Image',files.name)
    Z.filename=files.name
    Z.users=1
    Z.power=power
    Z.save()
    return render(request,'ok.html')
def look(request,id):
    Z=Information.objects.get(pk=int(id))
    context={'list':Z}
    return render(request,'look.html',context)
def ok(request):
    return render(request,'Z1.html')
def shenhe(request):
    list=Information.objects.all()
    content={'list':list}
    return render(request,'shenhe.html',content)
def shenhe_handler(request):
    id=request.POST.get('id')
    list=Information.objects.get(pk=int(id))
    state=request.POST.get('state')
    print(state)
    list.state=state
    list.save()
    return render(request,'cg.html')
def cg(request):
    return render(request,'cg.html')
def change(request,id):
	b=Information.objects.get(pk=int(id))
	context={'bo':b}
	return render(request,'change.html',context)
def change_handler(request):
	title=request.POST.get('title')
	content=request.POST.get('content')
	id=request.POST.get('id')
	b=Information.objects.get(pk=int(id))
	b.title=title
	b.content=content
	b.save()
	return render(request,'ok.html')
def del_handler(request,id):
	d=Information.objects.get(pk=int(id))
	d.delete()
	return HttpResponse('<h1>success</h1><a href="/Z1/">back</a>')
